﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data;
using System.Data.SqlClient;
using Dapper;
using System.Threading;
using LRT.Syndication.Business;
using LRT.Nebula.Xml;
using LRT.Syndication.Components;
using LRT.Syndication.Business;
using LRT.Syndication.Data;
using System.Xml;

namespace LRT.ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var rss = new XmlDocument();
            rss.Load("http://feeds.arstechnica.com/arstechnica/index/");

            var channel = RSSFeedVisitor.GetChannel(rss);
            var items = RSSFeedVisitor.GetItems(rss);

            var channelService = new ChannelService();
            var itemService = new ItemService();

            channelService.InsertChannel(channel);
            foreach (var item in items)
            {
                itemService.InsertItem(item);
            }
            

            //var channel = RSSFeedVisitor.GetChannel("http://www.downg.com/xml/rss_Down_s5.xml");
            //Console.WriteLine("Title: " + channel.Title);
            //Console.WriteLine("Title: " + channel.Link);
            //Console.WriteLine("Title: " + channel.Description);
            //Console.WriteLine("Title: " + channel.LastBuildDate);

            //var person = XmlHelper.Deserialize<Person>(XmlTest.xmlstr);
            //Console.WriteLine("Title: " + person.Name);
            //Console.WriteLine("Title: " + person.Sex);
            //Console.WriteLine("Title: " + person.Age);
            //Console.WriteLine("Title: " + person.Tel);
            Console.ReadKey();
        }


        #region Thread Lock Text

        public class LockSingleTest
        {
            static object syncObject = new Object();
            static volatile LockSingleTest instanse = null;
            public static LockSingleTest Instanse
            {
                get
                {
                    
                    lock (syncObject)
                    {
                        //Monitor.Wait(syncObject);
                        if(instanse == null)
                            instanse = new LockSingleTest();
                        //Monitor.Pulse(syncObject);
                        return instanse;
                        
                    }
                }
            }

        }

        #endregion



        #region DapperTest

        public void DapperTest()
        { 
            IDbConnection connection = new SqlConnection("Data Source=(local);initial catalog=lxtalk1;Integrated Security=false;User ID=sa;Password=zjwskj;");

            var sql = @"SELECT * FROM [Employees];
                        SELECT * FROM [Orders];
                        SELECT * FROM [Products];
                        SELECT * FROM [Customers];";
            using(var gridReader = connection.QueryMultiple(sql))
            {
                var employee = gridReader.Read<Employees>().First();
                var orders = gridReader.Read<Orders>().ToList();
                //var employee = gridReader
                //    gridReader.Read<User,Customer,Department,Order>(e => e.Name)
            }

            
        }

        #endregion


        #region StopwatchTest

        public void StopwatchTest()
        {
            var sw = new Stopwatch();
            sw.Start();

            Console.WriteLine("for loop start 10,000~2");
            for (int i = 0; i < 10000 * 10000; i++)
            {
                var num = 1;
            }
            sw.Stop();
            Console.WriteLine("time:" + sw.ElapsedMilliseconds);


            sw.Restart();
            Console.WriteLine("for loop start 100,0~2");
            Int64 t = 1000;
            for (int i = 0; i < t * t; i++)
            {
                var num = 1;
            }
            sw.Stop();
            Console.WriteLine("time:" + sw.ElapsedMilliseconds);


            sw.Restart();
            Int64 t2 = 1000000;
            Console.WriteLine("for loop start 1000,000");
            for (int i = 0; i < t2 * t2; i++)
            {
                var num = 1;
            }
            sw.Stop();
            Console.WriteLine("time:" + sw.ElapsedMilliseconds);

            Console.ReadKey();
        }

        #endregion
    }

    class Employees
    {
        public int EmployeeID { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }
    }

    class Orders
    {
        public int OrderID { get; set; }

        public string CustomerID { get; set; }

        public DateTime OrderDate { get; set; }
    }

    class Products
    {
        public int ProductID { get; set; }

        public string ProductName { get; set; }

        public decimal UnitPrice { get; set; }
    }

    class Customers
    {
        public int CustomerID { get; set; }

        public string ContactName { get; set; }

        public string Phone { get; set; }
    }
}
